
/****************************************************************************/
/*     framebuffer.cpp     Various functions for the framebuffer class      */
/*                         By: Josh Fiorio                                  */
/****************************************************************************/

#include "framebuffer.h"

extern long pcolor;

void FrameBuffer::Init(int w, int h)
{
	width = w;
	height = h;
	bpp = 4;
	framebuffer = (unsigned char*)malloc(width*height*bpp);
	memset(framebuffer, RGB(200,200,200), width*height*bpp);
} //end of func.

void FrameBuffer::Init(int w, int h, int mag)
{
	width = w*mag;
	height = h*mag;
	bpp = 4;
	framebuffer = (unsigned char*)malloc(width*height*bpp);
	memset(framebuffer, RGB(200,200,200), width*height*bpp);
} //end of func.

void FrameBuffer::SetPixel(int x, int y, unsigned int color) 
{  
    unsigned int r, g, b;
    if (x<width && y<height) 
	{
        b = (color>>16); 
        g = (color>>8); 
        r = (color & 0xff); 
		
	    if (x>=0 && y>=0)
        {
            framebuffer[bpp*(x+(height-1-y)*width)+0] = b; 
	        framebuffer[bpp*(x+(height-1-y)*width)+1] = g; 
            framebuffer[bpp*(x+(height-1-y)*width)+2] = r; 
        }   //end if
	}   //end if
} //end of func.

long FrameBuffer::GetPixel(int x, int y)
{
    if (x<width && y<height && x>=0 && y>=0) 
	{
		long color=0;
		BYTE r,g,b=0;

		b=*(framebuffer+(bpp*(x+(height-1-y)*width)+0));
		g=*(framebuffer+(bpp*(x+(height-1-y)*width)+1)); 
	    r=*(framebuffer+(bpp*(x+(height-1-y)*width)+2)); 

		color=RGB(r,g,b);
		return color;
	}	//end if

	return -1;
} //end of func.

void FrameBuffer::Clear()
{
	memset(framebuffer,200,width*height*bpp);
}	//end of func.

void CopyBuf(FrameBuffer &b1, FrameBuffer &b2)
{
    long pix = 0;
	for (int ylp=0; ylp<=b2.height; ylp++)
	{
		for (int xlp=0; xlp<=b2.width; xlp++)
		{
            pix = b1.GetPixel(xlp, ylp);
            
            if (pix >= 0)
			   b2.SetPixel(xlp, ylp, pix);
		}	//end for
	}	//end for
}	//end of func.

void MagBuf(FrameBuffer &b1, int oldmag, FrameBuffer &b2, int newmag)
{
	//Initialize second buffer
	//b2.Init((b1.width/oldmag)*newmag, (b1.height/oldmag)*newmag);

	//copy the new stuff onto it
	for (int ylp=0; ylp<=b1.height/oldmag; ylp++)
	{
		for (int xlp=0; xlp<=b1.width/oldmag; xlp++)
		{
			for (int yp=0; yp<newmag; yp++)
			{
				for (int xp=0; xp<newmag; xp++)
				{
					b2.SetPixel (((xlp)*newmag)+xp, ((ylp)*newmag)+yp, b1.GetPixel(xlp*oldmag, ylp*oldmag)); //RGBPal(palettes(pal_on), it.lines(ylp).pixels(pixon))
				}	//end for
			}	//end for
		}	//end for
	}	//end for

}	//end of func.

void ShiftBuf(FrameBuffer &it, long x, long y)
{
	//Declare and init a temp. buffer
	FrameBuffer newBuf;
	newBuf.Init(it.width, it.height);
	long pix = 0;

	//Copy pixels into temp buf.
	for (int ylp=0-y; ylp <= it.height-y; ylp++)
	{
		for (int xlp=0-x; xlp <= it.width-x; xlp++)
		{
			pix = it.GetPixel(xlp, ylp);
			if (pix==-1) {pix=pcolor;}
			newBuf.SetPixel(xlp+x, ylp+y, pix);
		}	//end for
	}	//end for

	//Copy temp buf. into buf
	CopyBuf(newBuf, it);
	newBuf.Delete();
}	//end of func.

